package com.guit.hrms.controller;

import com.guit.hrms.po.Role;
import com.guit.hrms.po.utilsPO.PageBean;
import com.guit.hrms.po.utilsPO.Result;
import com.guit.hrms.service.QueryService;
import com.guit.hrms.utils.ThreadLocalUtil;
import com.guit.hrms.vo.EmployeeVO;
import jakarta.validation.constraints.NotBlank;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@Slf4j
@CrossOrigin
@RestController
@RequestMapping("/api")
public class QueryController {

    @Autowired
    private QueryService queryService;

    /**
     * 查询所有员工
     * @return 所有员工信息
     */
    @GetMapping("/getEmpInfo")
    public Result getEmpInfo(@RequestParam(defaultValue = "1") Integer page ,
                             @RequestParam(defaultValue = "10") Integer pageSize,
                             Integer department, Integer deptType,
                             Integer position){
        PageBean employeeList = queryService.query(page, pageSize, department, deptType, position);
//        List<EmployeeVO> employeeVOList = new ArrayList<>();
//        String deptName = null;
//        String role = null;
//        EmployeeVO employeeVO;
//        for (Employee employee : (List<Employee>)employeeList.getRow()) {
//            employeeVO = new EmployeeVO();
//            deptName = queryService.getDepartment(employee);
//            role = queryService.getRoleById(employee.getEmployeeId());
//            BeanUtils.copyProperties(employee, employeeVO);
//            employeeVO.setDeptName(deptName);
//            employeeVO.setRole(role);
//            employeeVOList.add(employeeVO);
//        }
//
//        employeeList.setRow(employeeVOList);
        return Result.success(employeeList);
    }

    /**
     * 获取单个员工信息
     * @param employeeId 员工号
     * @return 单个员工信息
     */
    @GetMapping("/getEmpInfo/{employeeId}")
    public Result getEmpInfoById(@PathVariable @NotBlank(message = "employeeId不能为空") String employeeId){
        EmployeeVO employeeVO = queryService.getById(employeeId);
        String role = queryService.getRoleById(employeeVO.getEmployeeId()).getName();
        employeeVO.setRole(role);
        return Result.success(employeeVO);
    }

    /**
     * 获取员工个人信息
     * @return 单个员工信息
     */
    @GetMapping("/getEmpInfo/personal")
    public Result getPersonalEmpInfo(){
        Map<String,Object> map = ThreadLocalUtil.get();
        String employeeId = map.get("employeeId").toString();

        EmployeeVO employeeVO = queryService.getById(employeeId);
        Role role = queryService.getRoleById(employeeVO.getEmployeeId());
        employeeVO.setRole(role.getName());
        employeeVO.setRoleId(role.getId());
        return Result.success(employeeVO);
    }
}
